//
//  EffectViewController.m
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 11/23/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

#import "EffectViewController.h"
#import "Utils.h"
#import "EAGLView.h"
#include "LBTexture.h"
#import "OCImage.h"

#define scrollBarWidth 320
#define scrollBarHeight 60
#define buttonWidth 80
#define buttonHeight scrollBarHeight
#define border 2
#define numButton 10

#define NUM_BUTTON_IMAGE 4
#define W_BUTTON_IMAGE 40
#define H_BUTTON_IMAGE 40
#define DISTANCE_BUTTON_IMAGE 10
#define FROM_BOTTOM_TO_GL_VIEW 60
#define STANDARD_BRIGHTNESS 0.1

#define W_BUTTON_MENU_BAR 48
#define H_BUTTON_MENU_BAR 40

@interface EffectViewController() {
    
@private
    ImageMainMenuBarView* actionBarEffectNew;
    ImageMainMenuBarView* actionBarFrame;
    EAGLView* eaglView;
    ImageActionBarView* actionBarMenu;
    int actionBarUsing;
    UIView* loadingView;
    UIView* changeSmoothValueView;
    UISlider* smoothSlider;
    UISlider* brightnessSlider;
    UISlider* sharpenSlider;
    UIImageView* indicator;
    bool isEnableChangeSmoothValue;
    UIImage* activeSkinIcon, *inactiveSkinIcon;
    bool isProcessDefaultValue;             // when input image, slider will set default value. So,
    CGRect cropRect;
    
    int effectButtonIndex;
    int rotateButtonIndex;
    int cropButtonIndex;
    int smoothSkinButtonIndex;
    
    UIImage* activeBackgroundImage;
    
    int stepRotate;
}

@property (nonatomic, retain) UIView* loadingView;
@property (nonatomic, retain) UIView* changeSmoothValueView;

- (void) addActivityIndicator;
- (void) removeActivityIndicator;
- (void) addChangeSmoothValueView;
- (void) removeChangeSmoothValueView;
- (void) labelTap;
- (void) showHideChangeSmoothValueView;
- (void) brightnessSliderValueChange:(id)sender;
- (void) blurSizeSliderValueChange:(id)sender;
- (void) sharpenSliderValueChange:(id)sender;
- (void) resetSmoothSkinValue;
- (void) resetWhenInputImage;
- (void) pickButtonTouched;
- (void) takeImageButtonTouched;
- (void) hideEffectBar:(bool)isHidden;
- (void) hideChangeSmoothValueView:(bool)isHidden;
@end

@implementation EffectViewController

@synthesize loadingView;
@synthesize changeSmoothValueView;
@synthesize inputImg;
@synthesize isEnableEffect;
@synthesize delegate;

- (id)init{
    if(self = [super init]){
        isHiddenEffectBar = true;
        actionBarUsing = 1;
        isCoverImage = NO;
        isEnableChangeSmoothValue = true;
        isEnableEffect = false;
        stepRotate = 0;
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    NSLog(@"Receive memory warning");
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    [super loadView];
    
    int width = [UIScreen mainScreen].bounds.size.width;
    int height = [UIScreen mainScreen].bounds.size.height;
    
    activeSkinIcon = [[UIImage imageNamed:@"icon_skin.png"] retain];
    inactiveSkinIcon = [[UIImage imageNamed:@"icon_skin_dis.png" ] retain];
    activeBackgroundImage = [[UIImage imageNamed:@"active_background.png"] retain];
    
    [self addNewActionBarMenu];
    
    eaglView = [[EAGLView alloc] initWithFrame:CGRectMake(0, 0, width, height - actionBarMenu.frame.size.height)];
    eaglView.animationInterval = 1.0/60.0;
    [eaglView setDelegate:self];
    //[eaglView startAnimation];
    [self.view addSubview:eaglView];
    [eaglView release];
    if (inputImg) {
        [self inputImage:inputImg isFirstTime:true];
    }
    
    [self addNewActionBarEffect];
    
    
    if(isEnableEffect){
        UIButton* bEffect = [actionBarMenu buttonAtIndex:0];
        UIImage* indicatorImage = [UIImage imageNamed:@"icon_effect_arrow.png"];
        indicator = [[UIImageView alloc] initWithFrame:CGRectMake(actionBarMenu.frame.origin.x + bEffect.frame.origin.x + (bEffect.frame.size.width - indicatorImage.size.width)/2, actionBarEffectNew.frame.origin.y + actionBarEffectNew.frame.size.height, indicatorImage.size.width, indicatorImage.size.height)];
        indicator.image = indicatorImage;
        [self.view addSubview:indicator];
        [indicator release];
    }
    
    UIButton* pickButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pickButton.frame = CGRectMake(0, 0, 60, 30);
    [pickButton addTarget:self action:@selector(pickButtonTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pickButton];
    
    UIButton* takeImageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    takeImageButton.frame = CGRectMake(80, 0, 60, 30);
    [takeImageButton addTarget:self action:@selector(takeImageButtonTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:takeImageButton];
}


- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo{
    
    [self inputImage:[Utils scaledToSizeWithRGBA:CGSizeMake(960, 960) image:image] isFirstTime:true];
    [picker dismissModalViewControllerAnimated:YES];
    
}

- (void) takeImageButtonTouched{
    UIImagePickerController* pickerVc = [[UIImagePickerController alloc] init];
    pickerVc.delegate = self;
    pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:pickerVc animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissModalViewControllerAnimated:YES];
}

- (void) pickButtonTouched{
    UIImagePickerController* pickerVc = [[UIImagePickerController alloc] init];
    pickerVc.delegate = self;
    [self presentModalViewController:pickerVc animated:YES];
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)addNewActionBarEffect
{
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    
    int iButtonWidth = 55;
    int iButtonHeight = 55;
    int topPadding = 4;
    
    UIImage *imageThumbBackground = [UIImage imageNamed: @"thumbnail-bar.png"];
    
    NSMutableArray* arrayImage = [[NSMutableArray alloc] init];
    for (int i = 0; i < 22; i++) {
        [arrayImage addObject:[UIImage imageNamed:@"Origin.png"]];
    }
    int iNumButton = arrayImage.count;
    
    NSMutableArray* arraySubName = [[NSMutableArray alloc] init];
    
    //Add thumbnail default
    [arraySubName addObject:@"0"];
    
    
    for (int i = 0; i < iNumButton - 1; i++) {
        NSString* strName = [NSString stringWithFormat:@"%d", (i+1)];
        [arraySubName addObject:strName];
    }
    
    //Add to bar item
    actionBarEffectNew = [[ImageMainMenuBarView alloc] initWithNumButton:iNumButton
                                                               arrayName:arraySubName
                                                        arrayImageNormal:arrayImage
                                                         arrayImageTouch:NULL
                                                              sizeButton:CGSizeMake(iButtonWidth, iButtonHeight) spaceButton:6
                                                               infoFrame:CGRectMake(0,screenFrame.size.height - actionBarMenu.frame.size.height - iButtonHeight - 2*topPadding, screenFrame.size.width, iButtonHeight + 2*topPadding)
                                                         imageBackground:imageThumbBackground
                                                                delegate:self
                                                            hasEffectOut:FALSE];

    
    [self.view addSubview:actionBarEffectNew];
    
    [arrayImage removeAllObjects];
    UX_RELEASE_SAFELY(arrayImage);
    
    [arraySubName removeAllObjects];
    UX_RELEASE_SAFELY(arraySubName);
}

-(void)addNewActionBarFrame{
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    
    int iNumButton = 12;
    int iButtonWidth = 55;
    int iButtonHeight = 55;
    int topPadding = 4;
    
    UIImage *imageThumbBackground = [UIImage imageNamed: @"thumbnail-bar.png"];
    NSMutableArray* arrayImage = [[NSMutableArray alloc] initWithObjects:
                                  [UIImage imageNamed:@"Origin.png"],
                                  [UIImage imageNamed:@"f1-thumbnail.png"],
                                  [UIImage imageNamed:@"f2-thumbnail.png"],
                                  [UIImage imageNamed:@"f3-thumbnail.png"],
                                  [UIImage imageNamed:@"f4-thumbnail.png"],
                                  [UIImage imageNamed:@"f5-thumbnail.png"],
                                  [UIImage imageNamed:@"f6-thumbnail.png"],
                                  [UIImage imageNamed:@"f7-thumbnail.png"],
                                  [UIImage imageNamed:@"f8-thumbnail.png"],
                                  [UIImage imageNamed:@"f9-thumbnail.png"],
                                  [UIImage imageNamed:@"f10-thumbnail.png"],
                                  [UIImage imageNamed:@"f11-thumbnail.png"],
                                  [UIImage imageNamed:@"f12-thumbnail.png"],
                                  nil];
    NSMutableArray* arraySubName = [[NSMutableArray alloc] init];
    
    //Add thumbnail default
    iNumButton++;
    [arraySubName addObject:@"0"];
    
    
    for (int i = 0; i < iNumButton - 1; i++) {
        NSString* strName = [NSString stringWithFormat:@"%d", (i+1)];
        [arraySubName addObject:strName];
    }
    
    //Add to bar item
    actionBarFrame = [[ImageMainMenuBarView alloc] initWithNumButton:iNumButton
                                                           arrayName:arraySubName
                                                    arrayImageNormal:arrayImage
                                                     arrayImageTouch:NULL
                                                          sizeButton:CGSizeMake(iButtonWidth, iButtonHeight) spaceButton:6
                                                           infoFrame:CGRectMake(0,screenFrame.size.height - actionBarMenu.frame.size.height - iButtonHeight - 2*topPadding, screenFrame.size.width, iButtonHeight + 2*topPadding)
                                                     imageBackground:imageThumbBackground
                                                            delegate:self
                                                        hasEffectOut:FALSE];
    
    
    [self.view addSubview:actionBarFrame];
    
    [arrayImage removeAllObjects];
    UX_RELEASE_SAFELY(arrayImage);
    
    [arraySubName removeAllObjects];
    UX_RELEASE_SAFELY(arraySubName);
}


- (void)finishedAnimationWhenTouchButton:(NSInteger)iIndexSelected
{
    if(actionBarEffectNew.isHidden == false)
        [eaglView effect:iIndexSelected];
    else if(actionBarFrame.isHidden == false){
        eaglView.frameIndex = iIndexSelected;
    }
}

- (void)beginAnimationWhenTouchDown:(NSInteger)iIndexSelected
{
    
}

-(void)addNewActionBarMenu
{
    int iSubBarHeight = 46;
    
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    UIImage* imageBackground = [UIImage imageNamed: @"bar1.png"];
    
    NSArray *imageNormal = nil;
    
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")){
        imageNormal = [[NSArray alloc] initWithObjects:
                       [UIImage imageNamed:@"icon_effect.png"],
                       //                       [UIImage imageNamed:@"icon_frame.png"],
                       [UIImage imageNamed:@"icon-xoaytrai.png"],
                       [UIImage imageNamed:@"icon-catvuong.png"],
                       activeSkinIcon,
                       nil];
    }
    else{
        imageNormal = [[NSArray alloc] initWithObjects:
                       [UIImage imageNamed:@"icon_effect.png"],
                       [UIImage imageNamed:@"icon-xoaytrai.png"],
                       [UIImage imageNamed:@"icon-catvuong.png"],
                       //                       [UIImage imageNamed:@"icon_frame.png"],
                       nil];
    }
    
    effectButtonIndex = 0;
    rotateButtonIndex = 1;
    cropButtonIndex = 2;
    smoothSkinButtonIndex = 3;
    
    int NumButtonSub = imageNormal.count;
    
    actionBarMenu = [[ImageActionBarView alloc] initWithKindButton:NumButtonSub
                                                       spaceButton:0
                                                       imageNormal:imageNormal
                                                    imagesSelected:nil
                                                        sizeButton:CGSizeMake(W_BUTTON_MENU_BAR, H_BUTTON_MENU_BAR)
                                                             frame:CGRectMake(0,screenFrame.size.height - iSubBarHeight, screenFrame.size.width, iSubBarHeight)
                                                   imageBackground:imageBackground
                                                          delegate:self];
    
    
    
    [self.view addSubview:actionBarMenu];
    
    [imageNormal release];
    imageNormal = nil;
    
    [actionBarMenu release];
}

-(void)addNewActionBarMenuNoEffect{
    int iSubBarHeight = 46;
    
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    UIImage* imageBackground = [UIImage imageNamed: @"bar1.png"];

    
    NSArray *imageNormal = nil;
    
    
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")){
        imageNormal = [[NSArray alloc] initWithObjects:
                       [UIImage imageNamed:@"icon-xoaytrai.png"],
                       activeSkinIcon,
                       nil];
    }
    else{
        imageNormal = [[NSArray alloc] initWithObjects:
                       [UIImage imageNamed:@"icon-xoaytrai.png"],
                       nil];
    }
    
    int NumButtonSub = imageNormal.count;
    
    actionBarMenu = [[ImageActionBarView alloc] initWithKindButton:NumButtonSub
                                                       spaceButton:0
                                                       imageNormal:imageNormal
                                                    imagesSelected:nil
                                                        sizeButton:CGSizeMake(W_BUTTON_MENU_BAR, H_BUTTON_MENU_BAR)
                                                             frame:CGRectMake(0,screenFrame.size.height - iSubBarHeight, screenFrame.size.width, iSubBarHeight)
                                                   imageBackground:imageBackground
                                                          delegate:self];
    
    
    
    [self.view addSubview:actionBarMenu];
    
    [imageNormal release];
    imageNormal = nil;
    
    [actionBarMenu release];
}

- (void)finishedActionSelected:(id)sender indexSelect:(int)iIndexSelected
{
    if (iIndexSelected == kTagLeft) {
        if(delegate && [delegate respondsToSelector:@selector(didProcessImage:sender:)]){
            [delegate didProcessImage:nil sender:self];
        }
    }
    else if(iIndexSelected == kTagRight){
        if(delegate && [delegate respondsToSelector:@selector(didProcessImage:sender:)]){
            [delegate didProcessImage:[eaglView getOutputImage] sender:self];
        }
        UIImageWriteToSavedPhotosAlbum([eaglView getOutputImage], nil, nil, nil);
    }
    else if(iIndexSelected - 2 == rotateButtonIndex){                     // Rotate
        stepRotate++;
        [eaglView rotate];
    }
    
    else if(iIndexSelected - 2 == effectButtonIndex){                       // Effect
        [self hideEffectBar:!actionBarEffectNew.hidden];
        [self hideChangeSmoothValueView:true];
    }
    else if(iIndexSelected - 2 == cropButtonIndex){                     // Crop
        CropViewController* cropController = [[[CropViewController alloc] init] autorelease];
        cropController.delegate = self;//cropRect
        cropController.inputImg = [eaglView getOriginImage];
        cropController.cropRectInit = cropRect;
        cropController.stepRotate = stepRotate;
        [self presentModalViewController:cropController animated:YES];
    }
    else if(iIndexSelected  - 2 == smoothSkinButtonIndex){              // smooth skin
        if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")){
            [self hideEffectBar:true];
            eaglView.isUseSmoothSkin = !eaglView.isUseSmoothSkin;
            if(eaglView.isUseSmoothSkin == true){
                [self addChangeSmoothValueView];
                [actionBarMenu updateImageButtonAtIndex:iIndexSelected-2 imageButton:activeSkinIcon];
                if(isProcessDefaultValue == false){
                    
                    [self resetSmoothSkinValue];
                    
                    [eaglView applySmoothSkinValue];
                    isProcessDefaultValue = true;
                }
            }
            else{
                [self removeChangeSmoothValueView];
                [actionBarMenu updateImageButtonAtIndex:iIndexSelected-2 imageButton:inactiveSkinIcon];
            }
        }
    }
    
}
- (void) hideEffectBar:(bool)isHidden{
    actionBarEffectNew.hidden = isHidden;
    if(actionBarEffectNew.hidden)
        [actionBarMenu updateBackgroundImageButtonAtIndex:effectButtonIndex imageButton:nil];
    else [actionBarMenu updateBackgroundImageButtonAtIndex:effectButtonIndex imageButton:activeBackgroundImage];
}

- (void)finishedActionSubItemSelected:(id)sender
                          indexSelect:(int)iIndexSelected
                   indexSubItemSelect:(int)iIndexSubItemSelect
{
    
}
- (void)slideValueChange:(NSInteger)newValue AtIndexSelect:(int)iIndexSelected
{
    
}



-(void)saveFile:(NSString*)filePath{
    [eaglView saveImageFinalTo:filePath];

}

-(void)dealloc{
     NSLog(@"RootVC.retainCount = %d", self.retainCount);
    [eaglView stopAnimation];
    [actionBarEffectNew release];
    NSLog(@"eaglView.retainCount = %d", eaglView.retainCount);
//    [eaglView release];
    [actionBarFrame release];
    [actionBarMenu release];
    [loadingView release];
    [changeSmoothValueView release];
    [activeSkinIcon release];
    [inactiveSkinIcon release];
    [inputImg release];
    [super dealloc];
}

- (void)inputImage:(UIImage *)image isFirstTime:(bool)isFirstTime{
    [eaglView inputImage:image isFirstTime:isFirstTime];
    [self resetWhenInputImage];
    [eaglView startAnimation];
}

-(void)didFinishRotate:(id)sender{
}

- (void) addActivityIndicator{
    int activityWidth = 100;
    int activityHeight = 100;
    self.loadingView = [[[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - activityWidth)/2, (self.view.frame.size.height - activityHeight)/2, activityWidth, activityWidth)] autorelease];
    loadingView.layer.cornerRadius = 5;
    loadingView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
    
    int indicatorWidth = 30;
    UIActivityIndicatorView* activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    activityIndicator.frame = CGRectMake((activityWidth - indicatorWidth)/2, (activityHeight - indicatorWidth)/2, indicatorWidth, indicatorWidth);
    [activityIndicator startAnimating];
    [loadingView addSubview:activityIndicator];
    
     
    
    [self.view addSubview:loadingView];
    
}

- (void) removeActivityIndicator{
    [loadingView removeFromSuperview];
}

- (void) addChangeSmoothValueView{
    int viewHeight = 90;
    
    if(changeSmoothValueView == nil){
        self.changeSmoothValueView = [[[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - actionBarMenu.frame.size.height - viewHeight, self.view.frame.size.width, viewHeight)] autorelease];
        changeSmoothValueView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
        
        int leftMargin = 10;
        int topMargin = 5;
        int labelWidth = 70;
        int labelHeight = 20;
        int sliderLeftMargin = leftMargin + labelWidth + 10;
        int sliderWidth = self.view.frame.size.width - sliderLeftMargin - 10;
        int numRow = 3;
        
        UILabel* brightnessLabel = [[UILabel alloc] initWithFrame:CGRectMake(leftMargin, topMargin, labelWidth, labelHeight)];
        brightnessLabel.text = @"Độ sáng da";
        brightnessLabel.backgroundColor = [UIColor clearColor];
        brightnessLabel.textColor = [UIColor whiteColor];
        brightnessLabel.font = [UIFont boldSystemFontOfSize:12];
        [changeSmoothValueView addSubview:brightnessLabel];
        [brightnessLabel release];
        
        brightnessSlider = [[UISlider alloc] initWithFrame:CGRectMake(sliderLeftMargin, topMargin, sliderWidth, labelHeight)];
        [brightnessSlider addTarget:self action:@selector(brightnessSliderValueChange:) forControlEvents:UIControlEventTouchUpInside];
        [brightnessSlider addTarget:self action:@selector(brightnessSliderValueChange:) forControlEvents:UIControlEventTouchUpOutside];
        brightnessSlider.minimumValue = 0.0;
        brightnessSlider.maximumValue = 0.3;
        [changeSmoothValueView addSubview:brightnessSlider];
        [brightnessSlider release];
        
        UILabel* smoothLabel = [[UILabel alloc] initWithFrame:CGRectMake(leftMargin, topMargin + viewHeight/numRow, labelWidth, labelHeight)];
        smoothLabel.text = @"Độ mịn da";
        smoothLabel.backgroundColor = [UIColor clearColor];
        smoothLabel.textColor = [UIColor whiteColor];
        smoothLabel.font = [UIFont boldSystemFontOfSize:12];
        [changeSmoothValueView addSubview:smoothLabel];
        [smoothLabel release];
        
        smoothSlider = [[UISlider alloc] initWithFrame:CGRectMake(sliderLeftMargin, topMargin + viewHeight/numRow, sliderWidth, labelHeight)];
        [smoothSlider addTarget:self action:@selector(blurSizeSliderValueChange:) forControlEvents:UIControlEventTouchUpInside];
        [smoothSlider addTarget:self action:@selector(blurSizeSliderValueChange:) forControlEvents:UIControlEventTouchUpOutside];
        smoothSlider.minimumValue = 0.0;
        smoothSlider.maximumValue = 3.0;
        [changeSmoothValueView addSubview:smoothSlider];
        [smoothSlider release];
        
        UILabel* sharpenLabel = [[UILabel alloc] initWithFrame:CGRectMake(leftMargin, topMargin + 2*viewHeight/numRow, labelWidth, labelHeight)];
        sharpenLabel.text = @"Độ sắc nét";
        sharpenLabel.backgroundColor = [UIColor clearColor];
        sharpenLabel.textColor = [UIColor whiteColor];
        sharpenLabel.font = [UIFont boldSystemFontOfSize:12];
        [changeSmoothValueView addSubview:sharpenLabel];
        [sharpenLabel release];
        
        sharpenSlider = [[UISlider alloc] initWithFrame:CGRectMake(sliderLeftMargin, topMargin + 2*viewHeight/3, sliderWidth, labelHeight)];
        [sharpenSlider addTarget:self action:@selector(sharpenSliderValueChange:) forControlEvents:UIControlEventTouchUpInside];
        [sharpenSlider addTarget:self action:@selector(sharpenSliderValueChange:) forControlEvents:UIControlEventTouchUpOutside];
        sharpenSlider.minimumValue = 0.0;
        sharpenSlider.maximumValue = 3.0;
        [changeSmoothValueView addSubview:sharpenSlider];
        [sharpenSlider release];
        
        [self resetSmoothSkinValue];
    }

    isEnableChangeSmoothValue = true;
    [self hideChangeSmoothValueView:false];
    [self.view addSubview:changeSmoothValueView];
}

- (void) removeChangeSmoothValueView{
    [changeSmoothValueView removeFromSuperview];
}

- (void)shouldFaceDetection:(id)sender{
    NSThread* thread = [[NSThread alloc] initWithTarget:self selector:@selector(addActivityIndicator) object:nil];
    [thread start];
    [thread release];
}

- (void)didFaceDetection:(id)sender{
    [self removeActivityIndicator];
}

- (void)eaglViewTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0") == true)
        if(actionBarEffectNew.hidden)
            [self showHideChangeSmoothValueView];
}

- (void) labelTap{
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0") == true)
        if(actionBarEffectNew.hidden)
            [self showHideChangeSmoothValueView];
}

- (void) showHideChangeSmoothValueView{
    if(eaglView.isUseSmoothSkin == false) return;
    if(isEnableChangeSmoothValue == true){
        changeSmoothValueView.hidden = false;
        isEnableChangeSmoothValue = false;
    }
    else{
        changeSmoothValueView.hidden = true;
        isEnableChangeSmoothValue = true;
    }
}

- (void) hideChangeSmoothValueView:(bool)isHidden{
    if(eaglView.isUseSmoothSkin == false) return;
    changeSmoothValueView.hidden = isHidden;
    isEnableChangeSmoothValue = !isHidden;
}

- (void) brightnessSliderValueChange:(id)sender{
    [eaglView setStandardBrightness:brightnessSlider.value];
    [eaglView applySmoothSkinValue];
}

- (void) blurSizeSliderValueChange:(id)sender{
    [eaglView setBlurSize:smoothSlider.value];
    [eaglView applySmoothSkinValue];
}

- (void) sharpenSliderValueChange:(id)sender{
    [eaglView setSharpness:sharpenSlider.value];
    [eaglView applySmoothSkinValue];
}

- (void) resetSmoothSkinValue{
    smoothSlider.value = [eaglView blurSizeOfBeautifulFilter];
    brightnessSlider.value = STANDARD_BRIGHTNESS;
    sharpenSlider.value = smoothSlider.value;
    [eaglView setBlurSize:smoothSlider.value];
    [eaglView setStandardBrightness:brightnessSlider.value];
    [eaglView setSharpness:sharpenSlider.value];
}

- (void) resetWhenInputImage{
    isEnableChangeSmoothValue = false;
    changeSmoothValueView.hidden = true;
    isProcessDefaultValue = false;}

- (void)didCropResultImage:(UIImage *)image cropRect:(CGRect)rect sender:(CropViewController *)sender{
    if(image){
        stepRotate = 0;
        [self inputImage:image isFirstTime:false];
        cropRect = rect;
    }
    
}
@end
